package com.shanzmoo.core.service.impl;

import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.core.config.pay.MyWxPayConfig;
import com.shanzmoo.core.domain.pay.PayOrderEntityLocalUtil;
import com.shanzmoo.core.domain.pay.WxPayOrderDto;
import com.shanzmoo.core.domain.pay.WxWapPayResp;
import com.shanzmoo.core.pay.sdk.wxpay.WXPay;
import com.shanzmoo.core.pay.sdk.wxpay.WXPayConstants;
import com.shanzmoo.core.service.IWxSpecialPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 微信特有支付服务接口
 * @author Gzhao 2021/1/2
 */
@Slf4j
@Service
public class WxSpecialPayServiceImpl implements IWxSpecialPayService {

    @Value("${trade.pay.wxPayCallBack}")
    private String wxNotifyUrl;

    @Override
    public SResponse<WxWapPayResp> unifiedOrderByJsapi(String openid) {
        try {
            WxPayOrderDto wxPayOrderDto = WxPayOrderDto.createByOpenid(PayOrderEntityLocalUtil.getPayOrderEntity(), openid);
            MyWxPayConfig config = new MyWxPayConfig();
            WXPay wxpay = new WXPay(config);

            Map<String, String> resp = wxpay.unifiedOrder(wxPayOrderDto.convertMap(wxNotifyUrl));

            WxWapPayResp wxWapPayResp = WxWapPayResp.createByMap(resp, config.getKey());

            if (wxWapPayResp != null) {
                log.info("下单成功：{}\n", resp);
                PayOrderEntityLocalUtil.setOrderStatus(true);

                return SResponse.ok(wxWapPayResp);
            } else {
                PayOrderEntityLocalUtil.setOrderStatus(false);
                return SResponse.fail(Constant.ERRCODE_LOGIC, "微信支付JSAPI下单失败: " + resp.get("return_msg"));
            }
        } catch (Exception e) {
            log.error("微信支付JSAPI下单失败",  e);
            PayOrderEntityLocalUtil.setOrderStatus(false);

            return SResponse.fail(Constant.ERRCODE_LOGIC, "微信支付JSAPI下单失败：" + e.getMessage());
        }
    }

    @Override
    public SResponse<String> unifiedOrderByMweb(String sceneInfo) {
        try {
            WxPayOrderDto wxPayOrderDto = WxPayOrderDto.createBySceneInfo(PayOrderEntityLocalUtil.getPayOrderEntity(), sceneInfo);
            MyWxPayConfig config = new MyWxPayConfig();
            WXPay wxpay = new WXPay(config);

            Map<String, String> resp = wxpay.unifiedOrder(wxPayOrderDto.convertMap(wxNotifyUrl));


            if( WXPayConstants.SUCCESS.equals(resp.get("result_code")) ){
                log.info("下单成功：{}\n", resp);
                PayOrderEntityLocalUtil.setOrderStatus(true);

                return SResponse.ok(resp.get("mweb_url"));
            } else {
                PayOrderEntityLocalUtil.setOrderStatus(false);
                return SResponse.fail(Constant.ERRCODE_LOGIC, "微信支付MWEB下单失败: " + resp.get("return_msg"));
            }
        } catch (Exception e) {
            log.error("微信支付MWEB下单失败",  e);
            PayOrderEntityLocalUtil.setOrderStatus(false);

            return SResponse.fail(Constant.ERRCODE_LOGIC, "微信支付MWEB下单失败：" + e.getMessage());
        }
    }
}
